package com.qianclass.talklive.app.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;

import com.orhanobut.logger.Logger;

import java.io.ByteArrayInputStream;

/**
 * 异步压缩图片
 */
public class CompressPhotoUtils {

    private ProgressDialog progressDialog;
    private ByteArrayInputStream mBais;


    public void CompressPhoto(Context context, Bitmap image, String imageName, CompressCallBack callBack) {
        CompressTask task = new CompressTask(context, image, imageName, callBack);
        task.execute();
    }



    class CompressTask extends AsyncTask<Void, Integer, Integer> {

        private CompressCallBack callBack;
        private Bitmap imagebitmap;
        private String imageName;
        private Context context;
        private String isSavePath;



        CompressTask(Context context, Bitmap imagebitmap, String imageName, CompressCallBack callBack) {
            this.context = context;
            this.callBack = callBack;
            this.imagebitmap = imagebitmap;
            this.imageName = imageName;
        }



        /**
         * 运行在UI线程中，在调用doInBackground()之前执行
         */
        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(context, null, "处理中...");
        }

        /**
         * 后台运行的方法，可以运行非UI线程，可以执行耗时的方法
         */
        @Override
        protected Integer doInBackground(Void... params) {

            try {
                //压缩图片
                //质量压缩图片
                mBais = ImageUtils.compressImage(imagebitmap);
                LogUtil.e("压缩图片", imagebitmap.toString());
                //imagebitmap = ImageUtils.decodeBitmap(context, imagebitmap);//宽高压缩图片
            }catch (Exception e){
                Logger.e(e.toString());

            }
            return null;
        }

        /**
         * 运行在ui线程中，在doInBackground()执行完毕后执行
         */
        @Override
        protected void onPostExecute(Integer integer) {
            progressDialog.dismiss();
            callBack.success(mBais);
        }

        /**
         * 在publishProgress()被调用以后执行，publishProgress()用于更新进度
         */
        @Override
        protected void onProgressUpdate(Integer... values) {
        }
    }


    public interface CompressCallBack {
        void success(ByteArrayInputStream byteArrayInputStream);
    }

}